package com.jolink.omiis.wechat.util;

import com.thoughtworks.xstream.XStream;

/**
 * 
 * xml-util
 * 
 * @author WangYanqing
 *
 * @param <T>
 */
public class XmlUtil<T> {

	public String obj2Xml(T t) {
		return obj2Xml(t, "xml");
	}

	public String obj2Xml(T t, String alias) {
		XStream xstream = new XStream();
		xstream.alias(alias, t.getClass());
		String xmlStr = xstream.toXML(t);
		return xmlStr;
	}

	/**
	 * @param xml
	 * @param clazz
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public T xml2Obj(String xml, Class<?> clazz) {
		XStream xstream = new XStream();
		xstream.omitField(clazz, "URL");
		xstream.omitField(clazz, "MsgId");
		xstream.omitField(clazz, "Event");
		xstream.omitField(clazz, "EventKey");
		xstream.omitField(clazz, "MenuId");
//		xstream.omitField(clazz, "Latitude_X");
//		xstream.omitField(clazz, "Latitude_Y");
//		xstream.omitField(clazz, "Latitude");
		xstream.aliasType("xml", clazz);
		return (T) xstream.fromXML(xml);
	}
}
